﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KPO_lab_02
{
    class PowHash
    {
        //константы
        int HASH_MIN = (int)'0' + (int)'0';
        int HASH_MAX = (int)'z' + (int)'z' + 50;

        string[] hashArray;

        private int countAdd;
        private int countFind;

        public int getCountAdd
        {
            get
            {
                return countAdd;
            }
        }

        public int getCountFind
        {
            get
            {
                return countFind;
            }
        } 

        public PowHash()
        {
            hashArray = new String[HASH_MAX - HASH_MIN]; 
        }

        private int getHash(string s)
        {
            try
            {
                return ((int)s[0] + (int)s[s.Length - 1] + s.Length) - HASH_MIN;
            }
            catch (IndexOutOfRangeException)
            {
                MessageBox.Show("Строка нулевой длины!");
                return 0;
            }
        }

        public void clear()
        {
            countAdd = 0;
            countFind = 0;
        }

        public bool addTableVar(string sName)
        {
            countAdd++;
            int hash = getHash(sName);
            if (hashArray[hash] == null)
            {
                hashArray[hash] = sName;
                return true;
            }
            else if (hashArray[hash] == sName)
            {
                return true;
            }
            else 
            {
                while (!addTableVar(sName, 1)) { };
                return true;
            }
        }

        public bool addTableVar(string sName, int count)
        {
            countAdd++;
            count++;
            int hash = (getHash(sName) + count * getHash(sName)) % (HASH_MAX - HASH_MIN);
            if (hashArray[hash] == null)
            {
                hashArray[hash] = sName;
                return true;
            }
            else if (hashArray[hash] == sName)
            {
                return true;
            }
            else
            {
                while (!addTableVar(sName, count)) { };
                return true;
            }
        }

        public bool getTableVar(string sName)
        {
            countFind = 1;
            int hash = getHash(sName);
            if (hashArray[hash] == null)
                return false;
            else if (hashArray[hash] == sName)
                return true;
            else 
            {
                while (!getTableVar(sName, 1)) { };
                return true;
            }
        }

        public bool getTableVar(string sName, int count)
        {
            count++;
            countFind++;
            int hash = (getHash(sName) + count * getHash(sName)) % (HASH_MAX - HASH_MIN);
            if (hashArray[hash] == null)
            {
                return false;
            }
            else if (hashArray[hash] == sName)
            {
                return true;
                }
            else
            {
                while (!getTableVar(sName, count)) { };
                return true;
            }
        }
    }
}
